using System;
using System.Data;
using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;

using PCSComSale.Order.DS;

namespace PCSComSale.Order.BO
{
	public class SOPackListManagementBO //:  ISOPackListManagementBO
	{
		private const string THIS = "PCSComSale.Order.BO.SOPackListManagementBO";
		public SOPackListManagementBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		//[AutoComplete(true)]
		public void Add(object pObjectVO)
		{
			try
			{
				SO_PackListMasterDS dsPackListMaster = new SO_PackListMasterDS();
				dsPackListMaster.Add(pObjectVO);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	
		public void Delete(object pObjectVO)
		{
			// TODO:  Add SOPackListManagementBO.Delete implementation

		}
	
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add SOPackListManagementBO.GetObjectVO implementation
			return null;
		}
	
		public void Update(object pObjectDetail)
		{
			// TODO:  Add SOPackListManagementBO.Update implementation

		}
	
		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  Add SOPackListManagementBO.UpdateDataSet implementation
		}

		//**************************************************************************              
		///    <Description>
		///       Get all data from SO_PackListMaster
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       TuanDM
		///    </Authors>
		///    <History>
		///       Thursday, January 20, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet List()
		{
			try
			{
				SO_PackListMasterDS dsPackListMaster = new SO_PackListMasterDS();
				return dsPackListMaster.List();
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to object Party by Code
		///    </Description>
		///    <Inputs>
		///        string pstrCode       
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       object
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, February 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public object GetObjectPartyVO(string pstrCode)
		{
			try
			{
				MST_PartyDS dsParty = new MST_PartyDS();
				return dsParty.GetObjectVO(pstrCode);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to get all list item to pack which satisfy by condition in pstrCondition
		///       comportable with SO_PackListDetail
		///    </Description>
		///    <Inputs>
		///        string pstrCondition       
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, February 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet SearchItemsByCondition(int pintMasLocID, DateTime pdtmSchedule, string pstrBeginSO, string pstrEndSO, int pintPartyID, bool pblnHasDate)
		{
			try
			{
				SO_CommitInventoryDetailDS dsCommitInventory = new SO_CommitInventoryDetailDS();
				return dsCommitInventory.ListItemsNotPackList(pintMasLocID, pdtmSchedule, pstrBeginSO, pstrEndSO, pintPartyID, pblnHasDate);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to get all list commit item in iventory to pack which satisfy by condition in pstrCondition
		///       Use to update fields after pack
		///    </Description>
		///    <Inputs>
		///        string pstrCondition       
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, February 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet SearchListItemCommitIventory(int pintMasLocID, DateTime pdtmSchedule, string pstrBeginSO, string pstrEndSO, int pintPartyID,  bool pblnHasDate)
		{
			try
			{
				SO_CommitInventoryDetailDS dsCommitInventory = new SO_CommitInventoryDetailDS();
				return dsCommitInventory.ListCommitIventory(pintMasLocID, pdtmSchedule, pstrBeginSO, pstrEndSO, pintPartyID, pblnHasDate);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to get all list commit item in iventory to pack which satisfy by condition in pstrCondition
		///       Use to update fields after pack
		///    </Description>
		///    <Inputs>
		///        int pintPackListMasterID       
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, February 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public DataSet ListCommitIventoryByPackListMaster(int pintPackListMasterID)
		{
			try
			{
				SO_CommitInventoryDetailDS dsCommitInventory = new SO_CommitInventoryDetailDS();
				return dsCommitInventory.ListCommitIventoryByPackListMaster(pintPackListMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       Update all information when users save
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, February 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public void UpdatePackList(object pobjPackListMaster, DataSet pdtsPackListDetail, DataSet pdtsCommitInventory, EnumAction enumAction)
		{
			try
			{
				SO_PackListMasterVO voPackListMaster = (SO_PackListMasterVO) pobjPackListMaster;
				string strPackListNo = ((SO_PackListMasterVO) pobjPackListMaster).PackListNo;
				SO_PackListMasterDS dsPackListMaster = new SO_PackListMasterDS();
				SO_PackListDetailDS dsPackListDetail = new SO_PackListDetailDS();
				SO_CommitInventoryDetailDS dsCommitInventory = new SO_CommitInventoryDetailDS();
				DataSet dstMiddle = new DataSet();
				dstMiddle.Tables.Add(SO_PackListDetailTable.TABLE_NAME);

				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_PackListDetailTable.COMMITQUANTITY_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_CommitInventoryDetailTable.LOCATIONID_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_CommitInventoryDetailTable.BINID_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_CommitInventoryDetailTable.PRODUCTID_FLD);

				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_CommitInventoryDetailTable.MASTERLOCATIONID_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_CommitInventoryDetailTable.COMMITINVENTORYDETAILID_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_DeliveryScheduleTable.SALEORDERDETAILID_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_PackListDetailTable.PACKLISTMASTERID_FLD);
				dstMiddle.Tables[SO_PackListDetailTable.TABLE_NAME].Columns.Add(SO_PackListDetailTable.PACKLISTDETAILID_FLD);
				switch (enumAction)
				{
				//Add new the PackList
				case EnumAction.Add:
					dsPackListMaster.AddFirstTimePackList(voPackListMaster);
					voPackListMaster.PackListMasterID = ((SO_PackListMasterVO) dsPackListMaster.GetObjectVO(strPackListNo)).PackListMasterID;
					int indrow =0;
					foreach (DataRow drow in pdtsPackListDetail.Tables[0].Rows)
					{
						if (drow.RowState != DataRowState.Deleted)
						{
							drow[SO_PackListMasterTable.PACKLISTMASTERID_FLD] = voPackListMaster.PackListMasterID;
							pdtsCommitInventory.Tables[0].Rows[indrow][SO_CommitInventoryDetailTable.PACKED_FLD] = true;
							DataRow drowData = dstMiddle.Tables[0].NewRow();
							foreach(DataColumn dcol in dstMiddle.Tables[0].Columns)
							{
								drowData[dcol.ColumnName] = drow[dcol.ColumnName];					
							}
							dstMiddle.Tables[0].Rows.Add(drowData);
						}
						indrow++;
					}
					dsPackListDetail.UpdateDataSet(dstMiddle);
					dsCommitInventory.UpdateDataSetForPackList(pdtsCommitInventory);
					break;
				//Update the PackList
				case EnumAction.Edit:
					dsPackListMaster.UpdateALittle(voPackListMaster);
					int ind =0;
					foreach (DataRow drow in pdtsPackListDetail.Tables[0].Rows)
					{
						if (drow.RowState == DataRowState.Deleted)
						{
							pdtsCommitInventory.Tables[0].Rows[ind][SO_CommitInventoryDetailTable.PACKED_FLD] = false;
						}
						ind++;
					}
					dsPackListDetail.UpdateDataSet(pdtsPackListDetail);
					dsCommitInventory.UpdateDataSetForPackList(pdtsCommitInventory);
					break;
				//Delete the PackList
				case EnumAction.Delete:
					dsPackListDetail.DeleteDetailForMaster(voPackListMaster.PackListMasterID);
					dsPackListMaster.Delete(voPackListMaster.PackListMasterID);
					foreach (DataRow drow in pdtsCommitInventory.Tables[0].Rows)
					{
						drow[SO_CommitInventoryDetailTable.PACKED_FLD] = false;
					}
					dsPackListDetail.UpdateDataSet(pdtsPackListDetail);
					dsCommitInventory.UpdateDataSetForPackList(pdtsCommitInventory);
					break;
				}
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       Use to get items for the PackMaster by Pack list number
		///    </Description>
		///    <Inputs>
		///       string pstrPackListNo       
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///		  DataSet
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, February 04, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//***************************************************************************
		//[AutoComplete(true)]
		public DataSet ListPackDetailByPackMaster(int pintPackListMasterID)
		{
			SO_PackListDetailDS dsPackListDetail = new SO_PackListDetailDS();
			try
			{
				return dsPackListDetail.ListPackDetailByPackMaster(pintPackListMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///		Create the query string to search pack list
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Tuesday, Mar 21, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		public string ConditionToSearchPackList(int pintMasterLocID, string pstrPackListNo)
		{
			string strCondition	= " WHERE " + SO_PackListMasterTable.TABLE_NAME + "." + SO_PackListMasterTable.PACKLISTNO_FLD +  " like '" + pstrPackListNo + 
				"' + '%' and " + SO_PackListMasterTable.TABLE_NAME + "." + MST_LocationTable.MASTERLOCATIONID_FLD + " = " + pintMasterLocID.ToString();

			return strCondition;
		}
		
		//**************************************************************************              
		///    <Description>
		///		Create the query string to search pack list
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       string
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Tuesday, Mar 21, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
        public string ConditionToSearchSaleOrder(int pintMasLocID, int pintCCNID, int pintPartyID)
		{
			string strWhere = string.Empty;
			strWhere = " WHERE " + SO_SaleOrderMasterTable.TABLE_NAME + "." + SO_SaleOrderMasterTable.CCNID_FLD + " = " + pintCCNID.ToString() + " and " +
				SO_SaleOrderMasterTable.TABLE_NAME + "." + SO_SaleOrderMasterTable.SHIPFROMLOCID_FLD + " = " + pintMasLocID
				+ " and (" + SO_SaleOrderMasterTable.TABLE_NAME + "." + SO_SaleOrderMasterTable.PARTYID_FLD + "=" + pintPartyID + " or " + pintPartyID + "=0)";
			return strWhere;
		}


		//**************************************************************************              
		///    <Description>
		///    Get information of PackList by PackList Number
		///    </Description>
		///    <Inputs>
		///    </Inputs>
		///    <Outputs>
		///    </Outputs>
		///    <Returns>
		///       object
		///    </Returns>
		///    <Authors>
		///       TuanDm
		///    </Authors>
		///    <History>
		///       Friday, Apr 29, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]
		public object GetPackListMasterByPackNo(string pstrPackListNo)
		{
			try
			{
				return new SO_PackListMasterDS().GetObjectVO(pstrPackListNo);	
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
